<template>
  <div class="app-container">
    <div class="nav-search">
      <el-input v-model="searchParams.productName" placeholder="搜索产品名称"></el-input>
      <el-date-picker
        v-model="searchParams.createDate"
        :clearable="false"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="产品创建日期"
      >
      </el-date-picker>
      <span>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-search" @click="toReset">重置</el-button>
      </span>
      <div class="tableHandle">
        <el-button class="filter-item" size="mini" type="success" @click="addProduct">新增产品</el-button>
      </div>
    </div>
    <div class="tableList">
      <el-table
        ref="table"
        v-loading="loading"
        stripe
        :data="tableData"
        size="small"
        style="width: 100%;"
      >
        <el-table-column prop="id" label="套餐编号"></el-table-column>
        <el-table-column prop="productName" label="产品名称"></el-table-column>
        <el-table-column label="产品状态">
          <template slot-scope="scope">
            <span>{{ scope.row.enabled == 1 ? "启用" : "停用" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createDate) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button v-if="scope.row.enabled == 1" size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">停用</el-button>
            <el-button v-else size="mini" type="success" @click="handleDelete(scope.$index, scope.row)">启用</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        background
        :page-size.sync="page.size"
        :total="total"
        :current-page.sync="page.page"
        style="margin-top: 8px; text-align: right;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      ></el-pagination>
    </div>
    <product-dialog :dialog-visible="dialogVisible" :params="productForm" :dialog-title="dialogTitle" :callback="callback"></product-dialog>
  </div>
</template>

<script>
import { query, edit } from '@/api/product';
import { mapGetters } from 'vuex';
import { parseTime } from '@/utils/index';
import productDialog from '@/views/oper/product/productDialog';

function InitParam() {
  return {
    'createBy': '',
    'intro': '', // 产品介绍
    'packageIds': [], // 选择套餐
    'phase': [], // 诊疗阶段
    'picturePath': '', // 图片路径
    'productName': '', // 产品名称
    'rules': [] // 诊疗建议
  };
}

export default {
  components: {
    productDialog
  },
  dicts: ['order_treat_stage', 'order_sub_stat', 'express_stat', 'sub_order_treat_stage', 'order_pay_flag', 'gender', 'record_stat', 'package_rule'],
  data() {
    return {
      loading: false,
      dialogVisible: false,
      dialogTitle: '',
      searchParams: {
        createDate: '', // 创建时间
        productName: '' // 产品名称
      },
      page: {
        page: 0,
        size: 10
      },
      tableData: [],
      total: 0,
      productForm: {
        'createBy': '',
        'intro': '', // 产品介绍
        'packageIds': [], // 选择套餐
        'phase': [], // 诊疗阶段
        'picturePath': '', // 图片路径
        'productName': '', // 产品名称
        'rules': [] // 诊疗建议
      }
    };
  },
  computed: {
    ...mapGetters(['searchInfo', 'user'])
  },
  watch: {},
  mounted() {
    this.getProductList({ pageIndex: 0, pageSize: 10 });
  },
  methods: {
    parseTime,
    // 获取列表
    getProductList(params) {
      this.loading = true;
      query(params)
        .then(res => {
          this.tableData = res.content || [];
          this.total = res.totalElements || 0;
          this.loading = false;
        })
        .catch(er => {
          this.loading = false;
        });
    },
    // 搜索
    toQuery() {
      const search = {
        pageIndex: this.page.page - 1,
        pageSize: this.page.size,
        ...this.searchParams
      };
      this.getProductList(search);
    },
    // 重置
    toReset() {
      this.searchParams.createDate = '';
      this.searchParams.productName = '';
      this.toQuery();
    },
    // 新增
    addProduct() {
      this.dialogVisible = true;
      this.dialogTitle = '新增产品';
    },
    // 编辑
    handleEdit(index, row) {
      this.dialogVisible = true;
      this.dialogTitle = '编辑产品';
      this.productForm = row;
      this.productForm.phase = row.phaseList || [];
      this.productForm.rules = row.rulesList || [];
      this.productForm.packageIds = row.packageIdList || [];
      this.productForm.productId = this.productForm.id;
    },
    // 停用
    handleDelete(index, row) {
      const param = JSON.parse(JSON.stringify(row));
      const msg = parseInt(param.enabled) === 1 ? '禁用' : '启用';
      param.productId = param.id;
      param.enabled = parseInt(param.enabled) === 1 ? 0 : 1;
      param.phase = param.phaseList;
      param.rules = param.rulesList;
      param.packageIds = param.packageIdList;
      delete param.id;
      delete param.createDate;
      delete param.phaseList;
      delete param.rulesList;
      delete param.packageIdList;
      this.$confirm('此操作将' + msg + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        edit(param).then(res => {
          this.$notify({
            title: '提示',
            message: msg + '成功',
            type: 'success',
            duration: 5000
          });
          this.getProductList({ pageIndex: 0, pageSize: 10 });
        });
      });
    },
    sizeChangeHandler() {
      this.toQuery();
    },
    // 页数变更
    pageChangeHandler(pageIndex) {
      this.toQuery();
    },
    callback(type) {
      if (type === '1') {
        this.getProductList({ pageIndex: 0, pageSize: 10 });
      }
      this.dialogVisible = false;
      this.productForm = new InitParam();
    }
  }
};
</script>
<style scoped lang='scss'>
.nav-search {
  padding: 10px 0;
  .el-input {
    width: 320px;
    margin-right: 10px;
  }
  .el-date-editor--date {
    width: 150px;
  }
}
.tableHandle {
  float: right;
  margin: 0;
  padding: 0;
  margin-top: 4px;
}
.searchForm {
  width: 49%;
  display: inline-block;
  margin: 5px 0px;
}
</style>
